package com.zhang.controller.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.dto.SkuSearchDto;
import com.zhang.common.model.SkuEsModel;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.goods.entity.SkuImags;
import com.zhang.goods.entity.SkuInfo;
import com.zhang.goods.service.ISkuImagsService;
import com.zhang.goods.service.ISkuInfoService;
import com.zhang.goods.vo.SkuSearchResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("sku")
@Slf4j
public class SkuController {
    @Autowired
    private ISkuImagsService skuImagsService;
    @Autowired
    private ISkuInfoService skuInfoService;

    /**
     * 获取sku的图集
     *
     * @param skuId
     * @return {@link R}
     */
    @GetMapping("atlas")
    public R getSkuAtlas(@RequestParam String skuId) {
        LambdaQueryWrapper<SkuImags> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuImags::getSkuId, skuId);
        List<SkuImags> list = skuImagsService.list(wrapper);
        List<HashMap<String, String>> collect = list.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("imgId", String.valueOf(item.getImgId()));
            map.put("isDefault", String.valueOf(item.getIsDefault()));//是否是默认，0默认，1不默认
            return map;
        }).collect(Collectors.toList());
        return RUtils.successData(collect);
    }

    /**
     * 获取最新上架的sku
     *
     * @param pageNum
     * @param pageSize
     * @return {@link R}
     */
    @GetMapping("new/list")
    public R getNewSkuList(@RequestParam int pageNum, @RequestParam int pageSize) {
        IPage<SkuInfo> page = skuInfoService.getNewSkuList(new Page<SkuInfo>(pageNum, pageSize));
        return RUtils.successData(page);
    }

    /**
     * 从ElasticSearch中查询/搜索sku
     *
     * @param skuSearchDto
     * @return {@link R}
     */
    @GetMapping("page/from/elasticSearch")
    R getSkuListFromES(SkuSearchDto skuSearchDto) {
        try {
            SkuSearchResultVO skuListFromES = skuInfoService.getSkuListFromES(skuSearchDto);
            return RUtils.successData(skuListFromES);
        } catch (IOException e) {
            log.error("发生错误" + e);
            return RUtils.err("查询失败");
        }
    }

    @GetMapping("list/advice")
    R getSkuListByAdvice(@RequestParam int pageNum, @RequestParam int pageSize) {
        List<SkuEsModel> skuListByAdvice = skuInfoService.getSkuListByAdvice(pageNum, pageSize);
        return RUtils.successData(skuListByAdvice);
    }

    /**
     * 获取该商店的其他商品
     *
     * @param skuId
     * @return {@link R}
     */
    @GetMapping("store/other/skus/{skuId}")
    public R getStoreOtherSku(@PathVariable String skuId) {
        List<SkuInfo> skuInfoList = skuInfoService.getStoreOtherSku(skuId);
        return RUtils.successData(skuInfoList);
    }
}
